let pageNum = 1;
let totalPage;
$(function () {
    $.ajax({
        url:"/orderManage/queryOrder",
        type:"post",
        datatype:"json",
        success:function (r) {
            console.log(r)
            if (r!=null) {
                var t = "";
                var i = 1;
                for (let j = 0; j < r.data.length; j++) {
                    let status = "";
                    let act = "";
                    if (r.data[j].status === "1") {
                        status = "审核中";
                        act += "<td>\n" +
                            "          \t<a href=\"orderView.html\" class=\"tablelink\" onclick='putOrderId(this)'>查看详情</a>\n" +
                            "          </td>";
                    }else if (r.data[j].status === "0"){
                        status = "未审核";
                        act += "<td>\n" +
                            "          \t<a href=\"orderView.html\" class=\"tablelink\" onclick='putOrderId(this)'>查看详情</a>\n" +
                            "            <a href=\"orderUpdate.html\" class=\"tablelink\">修改</a>\n" +
                            "            <a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"deltipOpen(this)\">删除</a>\n" +
                            "            <a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"examinetipOpen(this)\">提交审核</a>\n" +
                            "            </td>";
                    }else if (r.data[j].status === "2"){
                        status = "审核通过";
                        act = "<td>\n" +
                            "          \t<a href=\"orderView.html\" class=\"tablelink\" onclick='putOrderId(this)'>查看详情</a>\n" +
                            "            <a onclick='putOrderId(this)' href=\"../../storage/delivery/deliveryView.html\" class=\"tablelink\">出库详情</a>\n" +
                            "          </td>";
                    }else if (r.data[j].status === "3"){
                        status = "审核未通过";
                        act += "<td>\n" +
                            "            <a href=\"orderUpdate.html\" class=\"tablelink\">修改</a>\n" +
                            "            <a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"examinetipOpen(this)\">提交审核</a>\n" +
                            "          </td>";
                    }
                    t += "<tr>\n" +
                        "          <td>" + (i++) + "</td>\n" +
                        "          <td class='orderid' id='" + r.data[j].orderid + "'>" + r.data[j].orderid + "</td>\n" +
                        "          <td>" + r.data[j].customname + "</td>\n" +
                        "          <td>" + r.data[j].phone + "</td>\n" +
                        "          <td>" + r.data[j].ordertime + "</td>\n" +
                        "          <td>" + r.data[j].totalprice + "</td>\n" +
                        "          <td>" + r.data[j].operatorname + "</td>\n" +
                        "          <td>" + status + "</td>\n" +
                        "          <td>" + r.data[j].checkname + "</td>\n" +
                        "          <td>" + r.data[j].checktime + "</td>\n" +
                        act+
                        "        </tr>";
                }
                $(".tablelist tbody").html(t);
                $(".paginList").html("<li class=\"paginItem\"><a href=\"javascript:;\"><span class=\"pagepre\" onclick=\"prevPage(this)\"></span></a></li><li class=\"paginItem\"><a href=\"javascript:;\"><span class=\"pagenxt\" onclick=\"nextPage(this)\"></span></a></li>")
                $(".message>i").eq(0).html(r.totalRows);
                $(".message>i").eq(1).html(r.pageNum);
                totalPage = r.totalPage;
            }else {
                $(".tablelist tbody").html("空");
            }
        }
    })
})

function queryOrderByCondition() {
    let orderid = $("input[name='orderid']").val();
    let customname = $("input[name='customname']").val();
    let startTime = $("input[name='startTime']").val();
    let endTime = $("input[name='endTime']").val();
    let minprice = $("input[name='minprice']").val();
    let maxprice = $("input[name='maxprice']").val();
    let status = $("select[name='status']").val();
    console.log(status)

    $.ajax({
        url:"/orderManage/queryOrder",
        type:"post",
        data:{"orderid":orderid,"customname":customname,"startTime":startTime,"endTime":endTime,"minprice":minprice,"maxprice":maxprice,"status":status},
        datatype:"json",
        success:function (r) {
            console.log(r)
            if (r!=null) {
                var t = "";
                var i = 1;
                for (let j = 0; j < r.data.length; j++) {
                    let status = "";
                    let act = "";
                    if (r.data[j].status === "1") {
                        status = "审核中";
                        act += "<td>\n" +
                            "          \t<a href=\"orderView.html\" class=\"tablelink\" onclick='putOrderId(this)'>查看详情</a>\n" +
                            "          </td>";
                    }else if (r.data[j].status === "0"){
                        status = "未审核";
                        act += "<td>\n" +
                            "          \t<a href=\"orderView.html\" class=\"tablelink\" onclick='putOrderId(this)'>查看详情</a>\n" +
                            "            <a href=\"orderUpdate.html\" class=\"tablelink\">修改</a>\n" +
                            "            <a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"deltipOpen(this)\">删除</a>\n" +
                            "            <a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"examinetipOpen(this)\">提交审核</a>\n" +
                            "            </td>";
                    }else if (r.data[j].status === "2"){
                        status = "审核通过";
                        act = "<td>\n" +
                            "          \t<a href=\"orderView.html\" class=\"tablelink\" onclick='putOrderId(this)'>查看详情</a>\n" +
                            "            <a onclick='putOrderId(this)' href=\"../../storage/delivery/deliveryView.html\" class=\"tablelink\">出库详情</a>\n" +
                            "          </td>";
                    }else if (r.data[j].status === "3"){
                        status = "审核未通过";
                        act += "<td>\n" +
                            "            <a href=\"orderUpdate.html\" class=\"tablelink\">修改</a>\n" +
                            "            <a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"examinetipOpen()\">提交审核</a>\n" +
                            "          </td>";
                    }
                    t += "<tr>\n" +
                        "          <td>" + (i++) + "</td>\n" +
                        "          <td class='orderid' id='" + r.data[j].orderid + "'>" + r.data[j].orderid + "</td>\n" +
                        "          <td>" + r.data[j].customname + "</td>\n" +
                        "          <td>" + r.data[j].phone + "</td>\n" +
                        "          <td>" + r.data[j].ordertime + "</td>\n" +
                        "          <td>" + r.data[j].totalprice + "</td>\n" +
                        "          <td>" + r.data[j].operatorname + "</td>\n" +
                        "          <td>" + status + "</td>\n" +
                        "          <td>" + r.data[j].checkname + "</td>\n" +
                        "          <td>" + r.data[j].checktime + "</td>\n" +
                        act+
                        "        </tr>";
                }
                $(".tablelist tbody").html(t);
                $(".message>i").eq(0).html(r.totalRows);
                $(".message>i").eq(1).html(r.pageNum);
                totalPage = r.totalPage;
            }else {
                $(".tablelist tbody").html("");
            }
        }
    })
}

function putOrderId(a) {
    let orderid = $(a).parents().siblings("td").eq(1).attr("id");
    $.ajax({
        url:"/orderManage/putOrderId",
        type:"post",
        data:{"orderid":orderid},
        success:function () {
            
        }
    })
}

function deleteOrder(a) {
    let orderid = $(".selected").attr("id");

    console.log(orderid)
    $.ajax({
        url:"/orderManage/deleteOrder",
        type:"post",
        data:{"orderid":orderid},
        datatype:"json",
        success:function (r) {
            console.log(r)
            if (r==1){
                alert("删除成功");
                location.reload();
            }else {
                alert("删除失败");
                location.reload();
            }
        }
    })
}

function queryJob(a) {
    let deptname = $(a).val();
    $.ajax({
        url:"/orderManage/queryJobByDept",
        type:"post",
        data:{"deptname":deptname},
        datatype:"json",
        success:function (r) {
            if (r!=null){
                let str = "<option>请选择</option>";
                for (let i = 0; i < r.length; i++) {
                    str += "<option>"+r[i].jobname+"</option>"
                }
                $(a).next().children(".dfselect").html(str);
            }
        }
    })
}

function queryNameByJob(a) {
    let jobname = $(a).val();
    $.ajax({
        url:"/orderManage/queryNameByJob",
        type:"post",
        data:{"jobname":jobname},
        datatype:"json",
        success:function (r) {
            console.log(r)
            if (r!=null){
                let str = "<option>请选择</option>";
                for (let i = 0; i < r.length; i++) {
                    str += "<option id='"+r[i].uid+"'>"+r[i].uname+"</option>"
                }
                $(a).parents().next().children(".dfselect").html(str);
            }
        }
    })
}

function subCheck(a) {
    let orderid = $(".subCheck").attr("id");
    let uid = $("select[name='uid'] option:selected").attr("id");


    $.ajax({
        url:"/orderManage/subCheck",
        type:"post",
        data:{"orderid":orderid,"uid":uid},
        datatype:"json",
        success:function (r) {
            if (r==1){
                alert("提交成功");
                location.reload();
            }else {
                alert("提交失败");
                location.reload();
            }
        }
    })
}

function nextPage() {
    if (pageNum<totalPage){
        pageNum++;
    }
    $.ajax({
        url:"/orderManage/queryOrder",
        type:"post",
        data:{"pageNum":pageNum},
        datatype:"json",
        success:function (r) {
            console.log(r)
            if (r!=null) {
                var t = "";
                var i = 1;
                for (let j = 0; j < r.data.length; j++) {
                    let status = "";
                    let act = "";
                    if (r.data[j].status === "1") {
                        status = "审核中";
                        act += "<td>\n" +
                            "          \t<a href=\"orderView.html\" class=\"tablelink\" onclick='putOrderId(this)'>查看详情</a>\n" +
                            "          </td>";
                    }else if (r.data[j].status === "0"){
                        status = "未审核";
                        act += "<td>\n" +
                            "          \t<a href=\"orderView.html\" class=\"tablelink\" onclick='putOrderId(this)'>查看详情</a>\n" +
                            "            <a href=\"orderUpdate.html\" class=\"tablelink\">修改</a>\n" +
                            "            <a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"deltipOpen(this)\">删除</a>\n" +
                            "            <a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"examinetipOpen(this)\">提交审核</a>\n" +
                            "            </td>";
                    }else if (r.data[j].status === "2"){
                        status = "审核通过";
                        act = "<td>\n" +
                            "          \t<a href=\"orderView.html\" class=\"tablelink\" onclick='putOrderId(this)'>查看详情</a>\n" +
                            "            <a onclick='putOrderId(this)' href=\"../../storage/delivery/deliveryView.html\" class=\"tablelink\">出库详情</a>\n" +
                            "          </td>";
                    }else if (r.data[j].status === "3"){
                        status = "审核未通过";
                        act += "<td>\n" +
                            "            <a href=\"orderUpdate.html\" class=\"tablelink\">修改</a>\n" +
                            "            <a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"examinetipOpen(this)\">提交审核</a>\n" +
                            "          </td>";
                    }
                    t += "<tr>\n" +
                        "          <td>" + (i++) + "</td>\n" +
                        "          <td class='orderid' id='" + r.data[j].orderid + "'>" + r.data[j].orderid + "</td>\n" +
                        "          <td>" + r.data[j].customname + "</td>\n" +
                        "          <td>" + r.data[j].phone + "</td>\n" +
                        "          <td>" + r.data[j].ordertime + "</td>\n" +
                        "          <td>" + r.data[j].totalprice + "</td>\n" +
                        "          <td>" + r.data[j].operatorname + "</td>\n" +
                        "          <td>" + status + "</td>\n" +
                        "          <td>" + r.data[j].checkname + "</td>\n" +
                        "          <td>" + r.data[j].checktime + "</td>\n" +
                        act+
                        "        </tr>";
                }
                $(".tablelist tbody").html(t);
                $(".message>i").eq(0).html(r.totalRows);
                $(".message>i").eq(1).html(r.pageNum);
            }else {
                $(".tablelist tbody").html("空");
            }
        }
    })
}
function prevPage() {
    if (pageNum>1){
        pageNum--;
    }
    $.ajax({
        url:"/orderManage/queryOrder",
        type:"post",
        data:{"pageNum":pageNum},
        datatype:"json",
        success:function (r) {
            console.log(r)
            if (r!=null) {
                var t = "";
                var i = 1;
                for (let j = 0; j < r.data.length; j++) {
                    let status = "";
                    let act = "";
                    if (r.data[j].status === "1") {
                        status = "审核中";
                        act += "<td>\n" +
                            "          \t<a href=\"orderView.html\" class=\"tablelink\" onclick='putOrderId(this)'>查看详情</a>\n" +
                            "          </td>";
                    }else if (r.data[j].status === "0"){
                        status = "未审核";
                        act += "<td>\n" +
                            "          \t<a href=\"orderView.html\" class=\"tablelink\" onclick='putOrderId(this)'>查看详情</a>\n" +
                            "            <a href=\"orderUpdate.html\" class=\"tablelink\">修改</a>\n" +
                            "            <a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"deltipOpen(this)\">删除</a>\n" +
                            "            <a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"examinetipOpen(this)\">提交审核</a>\n" +
                            "            </td>";
                    }else if (r.data[j].status === "2"){
                        status = "审核通过";
                        act = "<td>\n" +
                            "          \t<a href=\"orderView.html\" class=\"tablelink\" onclick='putOrderId(this)'>查看详情</a>\n" +
                            "            <a onclick='putOrderId(this)' href=\"../../storage/delivery/deliveryView.html\" class=\"tablelink\">出库详情</a>\n" +
                            "          </td>";
                    }else if (r.data[j].status === "3"){
                        status = "审核未通过";
                        act += "<td>\n" +
                            "            <a href=\"orderUpdate.html\" class=\"tablelink\">修改</a>\n" +
                            "            <a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"examinetipOpen(this)\">提交审核</a>\n" +
                            "          </td>";
                    }
                    t += "<tr>\n" +
                        "          <td>" + (i++) + "</td>\n" +
                        "          <td class='orderid' id='" + r.data[j].orderid + "'>" + r.data[j].orderid + "</td>\n" +
                        "          <td>" + r.data[j].customname + "</td>\n" +
                        "          <td>" + r.data[j].phone + "</td>\n" +
                        "          <td>" + r.data[j].ordertime + "</td>\n" +
                        "          <td>" + r.data[j].totalprice + "</td>\n" +
                        "          <td>" + r.data[j].operatorname + "</td>\n" +
                        "          <td>" + status + "</td>\n" +
                        "          <td>" + r.data[j].checkname + "</td>\n" +
                        "          <td>" + r.data[j].checktime + "</td>\n" +
                        act+
                        "        </tr>";
                }
                $(".tablelist tbody").html(t);
                $(".message>i").eq(0).html(r.totalRows);
                $(".message>i").eq(1).html(r.pageNum);
            }else {
                $(".tablelist tbody").html("空");
            }
        }
    })
}
function nextPage1() {
    if (pageNum<totalPage){
        pageNum++;
    }
    let orderid = $("input[name='orderid']").val();
    let customname = $("input[name='customname']").val();
    let startTime = $("input[name='startTime']").val();
    let endTime = $("input[name='endTime']").val();
    let minprice = $("input[name='minprice']").val();
    let maxprice = $("input[name='maxprice']").val();
    let status = $("select[name='status']").val();
    console.log(status)

    $.ajax({
        url:"/orderManage/queryOrder",
        type:"post",
        data:{"pageNum":pageNum,"orderid":orderid,"customname":customname,"startTime":startTime,"endTime":endTime,"minprice":minprice,"maxprice":maxprice,"status":status},
        datatype:"json",
        success:function (r) {
            console.log(r)
            if (r!=null) {
                var t = "";
                var i = 1;
                for (let j = 0; j < r.data.length; j++) {
                    let status = "";
                    let act = "";
                    if (r.data[j].status === "1") {
                        status = "审核中";
                        act += "<td>\n" +
                            "          \t<a href=\"orderView.html\" class=\"tablelink\" onclick='putOrderId(this)'>查看详情</a>\n" +
                            "          </td>";
                    }else if (r.data[j].status === "0"){
                        status = "未审核";
                        act += "<td>\n" +
                            "          \t<a href=\"orderView.html\" class=\"tablelink\" onclick='putOrderId(this)'>查看详情</a>\n" +
                            "            <a href=\"orderUpdate.html\" class=\"tablelink\">修改</a>\n" +
                            "            <a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"deltipOpen(this)\">删除</a>\n" +
                            "            <a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"examinetipOpen(this)\">提交审核</a>\n" +
                            "            </td>";
                    }else if (r.data[j].status === "2"){
                        status = "审核通过";
                        act = "<td>\n" +
                            "          \t<a href=\"orderView.html\" class=\"tablelink\" onclick='putOrderId(this)'>查看详情</a>\n" +
                            "            <a onclick='putOrderId(this)' href=\"../../storage/delivery/deliveryView.html\" class=\"tablelink\">出库详情</a>\n" +
                            "          </td>";
                    }else if (r.data[j].status === "3"){
                        status = "审核未通过";
                        act += "<td>\n" +
                            "            <a href=\"orderUpdate.html\" class=\"tablelink\">修改</a>\n" +
                            "            <a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"examinetipOpen()\">提交审核</a>\n" +
                            "          </td>";
                    }
                    t += "<tr>\n" +
                        "          <td>" + (i++) + "</td>\n" +
                        "          <td class='orderid' id='" + r.data[j].orderid + "'>" + r.data[j].orderid + "</td>\n" +
                        "          <td>" + r.data[j].customname + "</td>\n" +
                        "          <td>" + r.data[j].phone + "</td>\n" +
                        "          <td>" + r.data[j].ordertime + "</td>\n" +
                        "          <td>" + r.data[j].totalprice + "</td>\n" +
                        "          <td>" + r.data[j].operatorname + "</td>\n" +
                        "          <td>" + status + "</td>\n" +
                        "          <td>" + r.data[j].checkname + "</td>\n" +
                        "          <td>" + r.data[j].checktime + "</td>\n" +
                        act+
                        "        </tr>";
                }
                $(".tablelist tbody").html(t);
                $(".message>i").eq(0).html(r.totalRows);
                $(".message>i").eq(1).html(r.pageNum);
            }else {
                $(".tablelist tbody").html("");
            }
        }
    })
}
function prevPage1() {
    if (pageNum>1){
        pageNum--;
    }
    let orderid = $("input[name='orderid']").val();
    let customname = $("input[name='customname']").val();
    let startTime = $("input[name='startTime']").val();
    let endTime = $("input[name='endTime']").val();
    let minprice = $("input[name='minprice']").val();
    let maxprice = $("input[name='maxprice']").val();
    let status = $("select[name='status']").val();
    console.log(status)

    $.ajax({
        url:"/orderManage/queryOrder",
        type:"post",
        data:{"pageNum":pageNum,"orderid":orderid,"customname":customname,"startTime":startTime,"endTime":endTime,"minprice":minprice,"maxprice":maxprice,"status":status},
        datatype:"json",
        success:function (r) {
            console.log(r)
            if (r!=null) {
                var t = "";
                var i = 1;
                for (let j = 0; j < r.data.length; j++) {
                    let status = "";
                    let act = "";
                    if (r.data[j].status === "1") {
                        status = "审核中";
                        act += "<td>\n" +
                            "          \t<a href=\"orderView.html\" class=\"tablelink\" onclick='putOrderId(this)'>查看详情</a>\n" +
                            "          </td>";
                    }else if (r.data[j].status === "0"){
                        status = "未审核";
                        act += "<td>\n" +
                            "          \t<a href=\"orderView.html\" class=\"tablelink\" onclick='putOrderId(this)'>查看详情</a>\n" +
                            "            <a href=\"orderUpdate.html\" class=\"tablelink\">修改</a>\n" +
                            "            <a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"deltipOpen(this)\">删除</a>\n" +
                            "            <a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"examinetipOpen(this)\">提交审核</a>\n" +
                            "            </td>";
                    }else if (r.data[j].status === "2"){
                        status = "审核通过";
                        act = "<td>\n" +
                            "          \t<a href=\"orderView.html\" class=\"tablelink\" onclick='putOrderId(this)'>查看详情</a>\n" +
                            "            <a onclick='putOrderId(this)' href=\"../../storage/delivery/deliveryView.html\" class=\"tablelink\">出库详情</a>\n" +
                            "          </td>";
                    }else if (r.data[j].status === "3"){
                        status = "审核未通过";
                        act += "<td>\n" +
                            "            <a href=\"orderUpdate.html\" class=\"tablelink\">修改</a>\n" +
                            "            <a href=\"javascript:void(0);\" class=\"tablelink\" onclick=\"examinetipOpen()\">提交审核</a>\n" +
                            "          </td>";
                    }
                    t += "<tr>\n" +
                        "          <td>" + (i++) + "</td>\n" +
                        "          <td class='orderid' id='" + r.data[j].orderid + "'>" + r.data[j].orderid + "</td>\n" +
                        "          <td>" + r.data[j].customname + "</td>\n" +
                        "          <td>" + r.data[j].phone + "</td>\n" +
                        "          <td>" + r.data[j].ordertime + "</td>\n" +
                        "          <td>" + r.data[j].totalprice + "</td>\n" +
                        "          <td>" + r.data[j].operatorname + "</td>\n" +
                        "          <td>" + status + "</td>\n" +
                        "          <td>" + r.data[j].checkname + "</td>\n" +
                        "          <td>" + r.data[j].checktime + "</td>\n" +
                        act+
                        "        </tr>";
                }
                $(".tablelist tbody").html(t);
                $(".message>i").eq(0).html(r.totalRows);
                $(".message>i").eq(1).html(r.pageNum);
            }else {
                $(".tablelist tbody").html("");
            }
        }
    })
}